<template>
  <div id="admin-manage" class="page-wrapper">
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <el-form class="form" :inline="true" ref="form" :model="formData">
          <el-form-item label="订单编号" prop="orderId">
            <el-input v-model.trim="formData.orderId" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="userName">
            <el-input v-model.trim="formData.userName" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="userPhone">
            <el-input v-model.trim="formData.userPhone" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="购买时间">
            <el-date-picker v-model="formData.time" type="datetimerange" :picker-options="pickerOptions"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right"></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" @click="handleSubmitClick">获取数据</el-button>
    </div>
    <div class="wrapper main-card">
      <el-table :data="pageData" style="width: 100%" :stripe="true" @selection-change="handleSelectionChange"
        row-key="id">
        <el-table-column prop="orderId" label="订单号"></el-table-column>
        <el-table-column prop="userName" label="姓名"></el-table-column>
        <el-table-column prop="userPhone" label="电话号"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称"></el-table-column>
        <!-- <el-table-column prop="goodsNumber" label="购买数量"></el-table-column> -->
        <el-table-column prop="payMoney" label="应付金额"></el-table-column>
        <el-table-column prop="userAddress" label="收货地址"></el-table-column>
        <el-table-column prop="trackingNumber" label="快递单号"></el-table-column>
        <el-table-column label="付款状态">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.paystate === 0 ? '未付款' :
                  scope.row.paystate === 1 ? '已付款' :
                    scope.row.paystate === 2 ? '待退款' :
                      scope.row.paystate === 3 ? '已退款' : '未知状态'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="订单状态">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.state === '0' ? '待付款' :
                  scope.row.state === '1' ? '待发货' :
                    scope.row.state === '2' ? '待收货' :
                      scope.row.state === '3' ? '已完成' : '未知状态'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间">
  <template slot-scope="scope">
    <span>{{ scope.row.createdAt | formatDate('YYYY-MM-DD HH:mm:ss') }}</span>
  </template>
</el-table-column>
   
        <!-- <el-table-column type="selection" width="55" /> -->
      </el-table>
      <el-dialog title="确认退款" :visible.sync="refundDialogVisible" width="30%">
        <p>您确定要为选中的订单进行批量退款吗？</p>
        <span slot="footer" class="dialog-footer">
          <el-button @click="refundDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmRefundOrder">确认退款</el-button>
        </span>
      </el-dialog>
      <!-- <div class="button-wrapper">
        <el-button @click="handleRefundOrder" type="primary"
          >一键退款</el-button
        >
      </div> -->
      <div class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="total"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import GroupSelector from '@/components/GroupSelector'
import { searchalreadyrefundorderlist } from '@/api/pcclinet'
import { allsend } from '@/api/pcclinet'
export default {
  name: 'RoomInfo',
  components: {
    GroupSelector
  },
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      refundDialogVisible: false, // 控制退款确认弹窗的显示
      selectedRows: [],

      formData: {
        orderId: '',
        userName: '',
        userPhone: '',
        time: []
      },
      modifydata: {},
      tableData: [],
      currentRow: null
    }
  },

  methods: {
    // Implement pagination methods
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = this.currentPage * this.pageSize
      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.pageData = this.queryByPage()
    },
    // Search button click handler
    handleSubmitClick() {
      const { orderId, userName, userPhone, time } = this.formData
      const strTime = time.length ? time[0] : ''
      const endTime = time.length ? time[1] : ''

      // Call API with form data
      searchalreadyrefundorderlist({ orderId, userName, userPhone, strTime, endTime })
        .then(res => {
          if (res.success) {
            this.tableData = res.data
            this.total = res.data.length
            this.pageData = this.queryByPage()
          }
        })
        .catch(err => {
          console.error('Error fetching data:', err)
        })
    },

    handleRefundOrder() {
      this.refundDialogVisible = true
    },
    confirmRefundOrder() {
      const selectedRows = this.tableData.filter(row => {
        return this.selectedRows.some(selected => selected.id === row.id)
      })
      //  console.log('选中的订单数据:', selectedRows)

      if (this.selectedRows.length === 0) {
        this.$message({
          message: '请至少选择一个订单进行退款',
          type: 'warning'
        })
        this.refundDialogVisible = false
        return
      }

      allsend(selectedRows) // 调用退款 API
        .then(res => {
          if (res.success) {
            this.$message({
              message: '退款成功',
              type: 'success'
            })
            // 刷新表格数据
            this.handleSubmitClick()
          }
        })
        .catch(err => {
          console.error('Error refunding orders:', err)
        })

      this.refundDialogVisible = false
    },
    // 监听选中行的变化
    handleSelectionChange(val) {
      this.selectedRows = val
    }
  },

  mounted() {
    // Initial data fetch
    searchalreadyrefundorderlist().then(res => {
      if (res.success) {
        //  console.log(res.data);

        this.tableData = res.data
        this.total = res.data.length
        this.pageData = this.queryByPage()
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}

.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* Vertical center alignment */
}

.list-wrapper {
  margin-top: 40px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}

.form-item-wrapper {
  display: flex;
  justify-content: space-between;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 10px;
  /* Adjust margin as needed */
}

.image-preview {
  max-width: 100px;
  /* Adjust as needed */
  max-height: 50px;
  /* Adjust as needed */
}

.button-wrapper {
  margin-top: 20px;
  /* Adjust margin as needed */
  display: flex;
  justify-content: flex-end;
  /* Align to right */
  align-items: center;
  /* Vertical center alignment */
}
</style>
